<template>
  <div>
    <!--   头部   -->
    <!--<mt-header title="注册">-->
    <!--<router-link to="/Login" slot="left">-->
    <!--<mt-button icon="back">返回</mt-button>-->
    <!--</router-link>-->
    <!--<mt-button icon="more" slot="right"></mt-button>-->
    <!--</mt-header>-->

    <!--  中间部分  -->
    <div class="Center">
      <div>
        <!--<img src="../assets/ico@1x11.png" alt="">-->
      </div>
      <p>填写验证码，直接登录</p>
      <p>验证码已发送至<span>{{num}}</span></p>
      <p>验证码</p>
      <input type="text" placeholder="请输入验证码" v-model="isMobileCode">
      <img src="../assets/cxfs.png" v-on:click="Againsend" v-show="show" class="count1">
      <span v-show="!show" class="count">{{count}}秒后重新发送</span>
      <p>设置昵称</p>
      <input type="text" placeholder="3-16位汉字、数字、字母及下划线" v-model="isUser">
      <p>设置密码</p>
      <input type="password" placeholder="6-16位密码，包含字母、数字" v-model="isPassword">
      <button @click="next">下一步</button>
    </div>
        <alertTip v-if="showAlert" @closeTip="go()" :alertText="alertText"></alertTip>

  </div>
</template>
<script>
  import Vue from 'vue';
  import Mint from 'mint-ui';
  import 'mint-ui/lib/style.css';
  import wx from 'weixin-js-sdk'
  import { wxHttpTool,wx_config, http_getParam, get_shopQrcoe,wx_share,wx_hideAllMenu } from '../components/httpUtil/httpUtil'
  import {msg_10046} from '../message/msg_10046'
  import {relogin} from "./httpUtil/httpUtil";

  Vue.use(Mint);
  export default {
    name: "RegisterCode",
    data() {
      return {
        isUser: "",
        isMobileCode: "",
        isPassword: "",
        num: "",
        show: true,
        count: '',
        timer: null,
        showAlert:false,
        alertText:null
      }
    },

    created: function () {
      wxHttpTool.wxAuthor(this, function (varRet, varJson, varThis = this) {
        if (varRet == -1) {
          varThis.alertTip('网络错误');
          varThis.go = function () {
            varThis.showAlert = false;
          }

        }
        // 关注钱应该判断下是否已经关注过了
        msg_10046.send(varThis,msg_10046.entity(),function(varThis,res){
          varThis.wxlist=res.data.p.sign
          wx_config(varThis.wxlist);
          varThis.hideLoading();
          wx_hideAllMenu();
          wx.hideMenuItems({
            //禁用单独分享朋友圈QQ空间等功能
            menuList: ["menuItem:share:timeline","menuItem:share:QZone","menuItem:share:qq"] // 要隐藏的菜单项，所有menu项见附录3
          });
        })


      },this.type)
      this.num = localStorage.getItem("tlp")
      console.log(this.num)
    },

    methods: {
      alertTip(msg){
        this.showAlert = true;
        this.alertText = msg;
      },
      go(){
        this.showAlert = false
        },
      // 点击按钮 提交数据
      next: function () {
        const jsonValue = JSON.stringify({
          "c": "10002",
          "p": {
            "name": this.num,  // 用户名
            "pw": this.isPassword,// 密码
            "platId": this.num, // 第三方平台ID
            "nickName": this.isUser,
            "mobileCode": this.isMobileCode, // 手机验证码
//            "invationCode": "as12f",  // 邀请码
            "tokenId": token()
          }
        });
        //   判断用户名 密码是否正确
        // 昵称：3-16位 可以为汉字、数字、字母（大小写）、下划线
        const isUser = /^[0-9a-zA-Z\u4e00-\u9fa5_]{3,16}$/;
        // 密码：6-16位密码，包含字母、数字
        const Password = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;

        if (isUser.test(this.isUser) && Password.test(this.isPassword)) {
          this.$http.post(api + '/qqs.us', jsonValue)
            .then((response) => {
            console.log(response)
              if (response.data.p.isTrue === true) {
                this.alertTip( "注册成功");
                this.go = function(){
                   this.showAlert = false;
                }
                // alert("注册成功")
                console.log(response)
                var cookieUtil = {
                  set: function (name, value, expires, path, domain, secure) {
                    var cookieText = encodeURIComponent(name) + '=' +
                      encodeURIComponent(value);
                    if (expires instanceof Date) {
                      cookieText += ';expires=' + expires.toGMTString();
                    }
                    if (path) {
                      cookieText += ';path=' + path;
                    }
                    if (domain) {
                      cookieText += ';domain=' + domain;
                    }
                    if (secure) {
                      cookieText += ';secure';
                    }
                    document.cookie = cookieText;
                  },
                  get: function (name) {
                    var cookieName = encodeURIComponent(name) + '=',
                      cookieStart = document.cookie.indexOf(cookieName),
                      cookieValue = null
                    if (cookieStart > -1) {
                      var cookieEnd = document.cookie.indexOf(';', cookieStart);
                      if (cookieEnd == -1) {
                        cookieEnd = document.cookie.length
                      }
                      cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd))
                    }
                    return cookieValue
                  },
                  unset: function (name, path, domain, secure) {
                    this.set(name, '', new Date(0), domain, path)
                  }
                }

                function setCookieDate(day) {
                  var date = null
                  if (typeof day == 'number' && day > 0) {
                    date = new Date()
                    date.setDate(date.getDate() + day)
                  } else {
                    throw new Error('!!')
                  }
                  return date
                }

                cookieUtil.set('name', this.isUser, setCookieDate(1))
                cookieUtil.set('password', this.isPassword, setCookieDate(1))
                cookieUtil.set('token', token(), setCookieDate(1))

                relogin(varThis=this)
              } else {
                this.alertTip(response.data.p.errorMsg);
                this.go = function(){
                   this.showAlert = false;
                }
                // alert(response.data.p.errorMsg)
                this.isPassword = ''
              }
            })
            .catch((error) => {
              console.log(error)
            });
        }
        else {
          this.alertTip("用户名或密码格式不正确");
          this.go = function(){
             this.showAlert = false;
          }
          // alert("用户名或密码格式不正确")
          this.isPassword = ''
        }
      },

      //  重新获取验证码
      Againsend: function () {
        // 点击按钮 提交接口信息
        let number = localStorage.getItem("tlp")
        const jsonValue = JSON.stringify({
          "c": "10003",
          "p": {
            "mobile": number
          }
        })
        this.$http.post(api + '/qqs.us', jsonValue)
          .then((response) => {
            console.log(response);
          })
          .catch((error) => {
            console.log(error);
          });
        // 倒计时   60s计时器
        const TIME_COUNT = 60;
        if (!this.timer) {
          this.count = TIME_COUNT;
          this.show = false;
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count <= TIME_COUNT) {
              this.count--;
            } else {
              this.show = true;
              clearInterval(this.timer);
              this.timer = null;
            }
          }, 1000)
        }
      }
    }
  }
</script>


<style scoped lang="scss">
  input {
    outline: none;
  }

  .count {
    position: fixed;
    left: 440px;
    top: 380px;
    font-size: 22px;
  }

  .mint-header {
    background: black;
  }

  .Center {
    width: 100%;
    background: #ffffff;
    text-align: center;
    position: relative;
    .count {
      font-size: 22px;
      color: #a1a1a1;
      position: absolute;
      margin-right: -30px;
    }
    .count1 {
      width: 94px;
      height: 24px;
      position: absolute;
      top: 387px;
      right: 12%;
    }
    div:nth-of-type(1) {
      width: 144px;
      height: 144px;
      border-radius: 50%;
      /*border: 1px solid #dcdcdc;*/
      left:0;
      top: 0;
      bottom: 0;
      right: 0;
      margin: auto;
      margin-top: 48px;
      position: relative;
      img {
        width: 84px;
        height: 84px;
        margin: auto;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
      }
    }
    p:nth-of-type(1) {
      font-size: 20px;
      color: #3b3b43;
      margin-top: 20px;
    }
    p:nth-of-type(2) {
      font-size: 28px;
      color: #a1a1a1;
      margin-top: 30px;
    }
    p:nth-of-type(3) {
      font-size: 24px;
      color: #3b3b43;
      margin-top: 60px;
    }
    div:nth-of-type(2) {
      width: 100%;
      display: flex;
      input {
        width:80%;
        text-align: center;
        padding-bottom: 26px;
        border: none;
        border-bottom: 1px solid #cdcdcd;
        font-size: 32px;
        margin-top: 30px;
        margin-left: 66px;
      }
      img {
        width: 94px;
        height: 24px;
        position: relative;
        left: -90px;
        top: 35px;
      }
    }
    p:nth-of-type(4) {
      font-size: 24px;
      color: #3b3b43;
      margin-top: 34px;
    }
    input {
      width: 80%;
      text-align: center;
      padding-bottom: 26px;
      border: none;
      border-bottom: 1px solid #cdcdcd;
      font-size: 32px;
      margin-top: 30px;
    }
    p:nth-of-type(5) {
      font-size: 24px;
      color: #3b3b43;
      margin-top: 30px;
    }
    button {
      width: 468px;
      height: 74px;
      border: none;
      border-radius: 30px;
      font-size: 28px;
      color: #FFFFFF;
      text-align: center;
      margin-top: 76px;
      outline: none;
      background: url("../assets/btn_pay@1x.png");
      background-size: 100%;
    }
  }
</style>
